<script>
	/**
	 * vuex管理登陆状态，具体可以参考官方登陆模板示例
	 */
	// import {
	// 	mapMutations
	// } from 'vuex';
	export default {
		// methods: {
		// 	...mapMutations(['login'])
		// },

		onLaunch: function() {
			// #ifdef APP-PLUS
			// uni.requireNativePlugin('fn-ad').initAd('a5f4a25e35daa3', '52009c8cc00bf67d328d68ecb7c76adf');
			var url = this.apiurl + "update"
			//console.log(url)
			let that = this;

			plus.runtime.getProperty(plus.runtime.appid, function(inf) {

				console.log(url);

				console.log('App Launch', inf.version);
				uni.request({
					url: url, //检查更新的服务器地址
					data: {
						appid: plus.runtime.appid,
						version: inf.version,
						imei: plus.device.imei
					},
					success: (res) => {
						console.log('rere', res)
						if (res.statusCode == 200 && res.data.isUpdate) {
							let openUrl = plus.os.name === 'iOS' ? res.data.iOS : res.data.Android;

							console.log("======================");
							console.log(res.data);

							// 提醒用户更新
							uni.showModal({
								title: '更新提示',
								showCancel: false,
								content: res.data.note ? res.data.note : '是否选择更新',
								success: (showResult) => {
									if (showResult.confirm) {
										that.downWgt(openUrl);
										// plus.runtime.openURL(openUrl);
									}
								}
							})
						}
					}
				})
				// uni.setStorageSync("_version",inf.version);

			})


			// 检测升级

			// #endif
		},

		onShow: function() {
			// #ifdef APP-PLUS
			//appediton = plus.runtime.version;	
			uni.setStorageSync('appediton', plus.runtime.version);
			// #endif

			// console.log('App Show123333333333333333333')
		},

		onHide: function() {
			// console.log('App Hide');
		},
		// onTabItemTap(e) {
		// 	console.log("tabbar的点击事件监听" + e);
			
		// },
		methods: {

			check() {
				var url = this.apiurl + "update"
				//console.log(url)
				let that = this;

				plus.runtime.getProperty(plus.runtime.appid, function(inf) {

					console.log('App Launch', inf.version);
					uni.request({
						url: url, //检查更新的服务器地址
						data: {
							appid: plus.runtime.appid,
							version: inf.version,
							imei: plus.device.imei
						},
						success: (res) => {
							console.log('rere', res)
							if (res.statusCode == 200 && res.data.isUpdate) {
								let openUrl = plus.os.name === 'iOS' ? res.data.iOS : res.data.Android;
								// 提醒用户更新
								uni.showModal({
									title: '更新提示',
									showCancel: false,
									content: res.data.note ? res.data.note : '是否选择更新',
									success: (showResult) => {
										if (showResult.confirm) {
											that.downWgt(openUrl);
											// plus.runtime.openURL(openUrl);
										}
									}
								})
							}
						}
					})
					// uni.setStorageSync("_version",inf.version);

				})
			},

			downWgt(wgtUrl) {
				console.log('进来了吗')
				var that = this;
				//var text = "正在下载更新文件..text.";
				// var wgtUrl="https://dingding.keweitianhui.com/public/uploads/9F37CF2.wgt";
				//var text = plus.nativeUI.showWaiting("开始下载更新...");
				var text = plus.nativeUI.showWaiting("开始下载更新...");

				const dtask = plus.downloader.createDownload(wgtUrl, {
					filename: "_doc/update/"
				}, function(d, status) {
					if (status == 200) {
						console.log("下载wgt成功：" + d.filename);
						that.installWgt(d.filename); // 安装wgt包  
					} else {
						console.log("下载wgt失败！");
						plus.nativeUI.alert("下载wgt失败！");
					}
					plus.nativeUI.closeWaiting();
				})

				dtask.addEventListener("statechanged", function(task, status) {
					var nowData = (task.downloadedSize * 100 / task.totalSize);
					var nowTxt = nowData.toFixed(0);

					if (nowData > 1) {
						if (nowTxt % 1 == 0) { // 让百分比 1% 增长，如果这里不这么处理  出现 堆栈内存溢出的问题，有知道原因的大神指导一下哈
							text.setTitle("　　 已下载: " + nowTxt + "%　　 ");
						}
					}

				}, false);

				dtask.start();
			},

			installWgt(path) {
				console.log('进来了吗', path)
				plus.nativeUI.showWaiting("安装wgt文件...");
				plus.runtime.install(path, {
					force: true
				}, function() {
					plus.nativeUI.closeWaiting();
					console.log("安装wgt文件成功！");
					plus.nativeUI.alert("恭喜,更新已完成!", function() {
						plus.runtime.restart();
					});
				}, function(e) {
					plus.nativeUI.closeWaiting();
					console.log("安装wgt文件失败[" + e.code + "]：" + e.message);
					plus.nativeUI.alert("安装wgt文件失败[" + e.code + "]：" + e.message);
				});
			},
		},

	}
</script>

<style lang="scss">
	@import "uview-ui/index.scss";
	@import "colorui/main.css";
	@import "colorui/icon.css";
</style>
